<template>
  <div class="theme-switch">
    <!-- 主题切换按钮 -->
    <el-dropdown @command="handleThemeChange" trigger="click" placement="bottom-end">
      <el-button type="text" class="theme-btn" title="换肤">
        <el-icon class="theme-icon">
          <Brush />
        </el-icon>
      </el-button>
      <template #dropdown>
        <el-dropdown-menu class="theme-dropdown">
          <div class="theme-header">
            <span class="header-title">换肤</span>
          </div>
          <div class="theme-grid">
            <div v-for="theme in themeList" :key="theme.value"
              :class="['theme-color-item', { active: currentTheme === theme.value }]"
              :style="{ backgroundColor: theme.primary }" @click="handleThemeChange(theme.value)"
              :title="`主题色: ${theme.primary}`">
              <div class="theme-check" v-if="currentTheme === theme.value">
                <el-icon>
                  <Check />
                </el-icon>
              </div>
            </div>
          </div>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { Brush, Check } from '@element-plus/icons-vue'
import {
  getAllThemes,
  getCurrentTheme,
  setTheme,
  onThemeChange
} from '@/utils/theme'
import { getInit } from '@/api/auth'
import { useUserStore } from '@/stores/user'
const userStore = useUserStore()
// 响应式数据
const currentTheme = ref(getCurrentTheme())
const themeList = ref(getAllThemes())

// 主题变更处理
const handleThemeChange = async (themeName) => {
  try {
    const success = await setTheme(themeName)
    if (success) {
      currentTheme.value = themeName
      getInitData();
    }
  } catch (error) {
    console.error('换肤失败:', error)
  }
}


// 获取初始化数据
const getInitData = async () => {
  try {
    const response = await getInit()
    const { employee_info, func_list, menu_list } = response.data || {}
    const init_option = response.data || {}

    userStore.setUserInfo({
      employee_info,
      func_list,
      menu_list,
      init_option
    })

    if (employee_info && employee_info.style) {
      initThemeFromUser(employee_info.style)
    }

    if (menu_list && Array.isArray(menu_list)) {
      forceSetupDynamicRoutes(menu_list)
    }
  } catch (error) {
    console.error('初始化失败:', error)
  }
}



// 监听主题变更
let unsubscribe = null

onMounted(() => {
  // 监听主题变更事件
  unsubscribe = onThemeChange((detail) => {
    currentTheme.value = detail.theme
  })
})

onUnmounted(() => {
  // 清理监听器
  if (unsubscribe) {
    unsubscribe()
  }
})
</script>

<style scoped>
.theme-switch {
  display: inline-block;
}

.theme-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  color: #ffffff !important;
  font-size: 14px;
  padding: 8px 12px;
  border-radius: 4px;
  transition: all 0.3s ease;
}

.theme-btn:hover {
  background-color: rgba(255, 255, 255, 0.1);
  color: var(--theme-header-text-color, #ffffff);
}

.theme-icon {
  font-size: 16px;
}

.theme-text {
  font-size: 13px;
}

/* 下拉菜单样式 */
.theme-dropdown {
  min-width: 240px;
  padding: 0;
  border-radius: 12px;
  box-shadow: var(--theme-shadow-dark);
  background: var(--theme-bg-primary);
}

.theme-header {
  padding: 16px 16px 12px;
  text-align: center;
  border-bottom: 1px solid var(--theme-border-light);
}

.header-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--theme-text-primary);
}

.theme-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 10px;
  padding: 16px;
  max-height: 240px;
  overflow-y: auto;
}

.theme-color-item {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid transparent;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

.theme-color-item:hover {
  transform: scale(1.1);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
}

.theme-color-item.active {
  border-color: #ffffff;
  box-shadow: 0 0 0 1px var(--theme-border-dark), 0 4px 12px rgba(0, 0, 0, 0.25);
  transform: scale(1.05);
}

.theme-check {
  color: #ffffff;
  font-size: 14px;
  font-weight: bold;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
}

/* 滚动条样式 */
.theme-grid::-webkit-scrollbar {
  width: 4px;
}

.theme-grid::-webkit-scrollbar-track {
  background: var(--theme-bg-secondary);
}

.theme-grid::-webkit-scrollbar-thumb {
  background: var(--theme-border-dark);
  border-radius: 2px;
}

.theme-grid::-webkit-scrollbar-thumb:hover {
  background: var(--theme-text-secondary);
}
</style>
